1.6 Arrays in C Programming

Module 1.6 • Contiguous Blocks, Matrix Arithmetic, Boundaries & Strings

1.6.1 Introduction to Arrays

In programming, there are many situations where we need to store multiple values of the same type. For example, storing marks of 50 students using separate variables would be inefficient:

int mark1, mark2, mark3, ... mark50;

To solve this problem, C provides arrays.

An array is a collection of elements of the same data type stored in consecutive memory locations and accessed using a common name.

Advantages of Arrays

1.6.2 Declaration of an Array

Syntax

data_type array_name[size];

Example

int scores[6];

This creates an integer array named scores capable of storing 6 values.

Memory Representation

Index Value
0scores[0]
1scores[1]
2scores[2]
3scores[3]
4scores[4]
5scores[5]

1.6.3 Array Indexing

Array indexing starts from 0.

Example

int scores[6] = {75, 82, 90, 68, 88, 95};
Index Value
075
182
290
368
488
595

Accessing:

printf("%d", scores[2]);

Output

90

1.6.4 Array Initialization

Arrays can be initialized while declaring.

Complete Initialization

int values[5] = {12, 24, 36, 48, 60};

Partial Initialization

int values[5] = {10, 20};

Stored values become:

10 20 0 0 0

Remaining elements automatically become zero.

1.6.5 Array Without Specifying Size

The compiler can determine array size automatically.

Example

int data[] = {11, 22, 33, 44, 55};

Array size becomes 5.

1.6.6 Input Values into an Array

Example Program

#include <stdio.h>
 
int main()
{
    int marks[5];
    int i;
 
    for(i = 0; i < 5; i++)
    {
        printf("Enter mark %d: ", i + 1);
        scanf("%d", &marks[i]);
    }
 
    printf("\nStored Marks:\n");
 
    for(i = 0; i < 5; i++)
    {
        printf("%d ", marks[i]);
    }
 
    return 0;
}

Sample Output

Enter mark 1: 78
Enter mark 2: 85
Enter mark 3: 92
Enter mark 4: 67
Enter mark 5: 88
 
Stored Marks:
78 85 92 67 88

1.6.7 Processing Arrays Using Loops

Arrays are commonly processed using loops.

Example: Print Elements

#include <stdio.h>
 
int main()
{
    int num[] = {4, 8, 12, 16, 20};
    int i;
 
    for(i = 0; i < 5; i++)
    {
        printf("%d ", num[i]);
    }
 
    return 0;
}

Output

4 8 12 16 20

1.6.8 Finding Sum of Array Elements

Example Program

#include <stdio.h>
 
int main()
{
    int values[6] = {5, 10, 15, 20, 25, 30};
    int i;
    int sum = 0;
 
    for(i = 0; i < 6; i++)
    {
        sum += values[i];
    }
 
    printf("Sum = %d", sum);
 
    return 0;
}

Output

Sum = 105

1.6.9 Finding Average of Array Elements

Example Program

#include <stdio.h>
 
int main()
{
    int marks[4] = {70, 80, 90, 100};
    int i;
    int total = 0;
    float average;
 
    for(i = 0; i < 4; i++)
    {
        total += marks[i];
    }
 
    average = total / 4.0;
 
    printf("Average = %.2f", average);
 
    return 0;
}

Output

Average = 85.00

1.6.10 Finding Maximum Element

Example Program

#include <stdio.h>
 
int main()
{
    int numbers[5] = {45, 72, 18, 99, 63};
    int max = numbers[0];
    int i;
 
    for(i = 1; i < 5; i++)
    {
        if(numbers[i] > max)
        {
            max = numbers[i];
        }
    }
 
    printf("Maximum = %d", max);
 
    return 0;
}

Output

Maximum = 99

1.6.11 Finding Minimum Element

Example Program

#include <stdio.h>
 
int main()
{
    int values[5] = {25, 14, 38, 7, 19};
    int min = values[0];
    int i;
 
    for(i = 1; i < 5; i++)
    {
        if(values[i] < min)
        {
            min = values[i];
        }
    }
 
    printf("Minimum = %d", min);
 
    return 0;
}

Output

Minimum = 7

1.6.12 Counting Even and Odd Numbers

Example Program

#include <stdio.h>
 
int main()
{
    int arr[8] = {4, 9, 12, 7, 16, 11, 20, 13};
    int even = 0, odd = 0;
    int i;
 
    for(i = 0; i < 8; i++)
    {
        if(arr[i] % 2 == 0)
            even++;
        else
            odd++;
    }
 
    printf("Even Numbers = %d\n", even);
    printf("Odd Numbers = %d\n", odd);
 
    return 0;
}

Output

Even Numbers = 4
Odd Numbers = 4

1.6.13 Reversing an Array

Example Program

#include <stdio.h>
 
int main()
{
    int arr[6] = {10, 20, 30, 40, 50, 60};
    int i;
 
    printf("Reverse Order:\n");
 
    for(i = 5; i >= 0; i--)
    {
        printf("%d ", arr[i]);
    }
 
    return 0;
}

Output

60 50 40 30 20 10

1.6.14 Searching in an Array (Linear Search)

Linear search checks elements one by one.

Example Program

#include <stdio.h>
 
int main()
{
    int arr[6] = {15, 22, 37, 48, 59, 63};
    int item = 48;
    int i;
 
    for(i = 0; i < 6; i++)
    {
        if(arr[i] == item)
        {
            printf("Item found at index %d", i);
            break;
        }
    }
 
    return 0;
}

Output

Item found at index 3

1.6.15 Passing Arrays to Functions

Arrays can be sent to functions.

Example Program

#include <stdio.h>
 
void display(int arr[], int size)
{
    int i;
 
    for(i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
}
 
int main()
{
    int data[] = {8, 16, 24, 32, 40};
 
    display(data, 5);
 
    return 0;
}

Output

8 16 24 32 40

1.6.16 Two-Dimensional Arrays

A two-dimensional array stores data in rows and columns.

Declaration

int matrix[3][4];

This creates:

Total elements: 3 × 4 = 12

1.6.17 Initializing a 2D Array

int matrix[3][3] =
{
    {5, 10, 15},
    {20, 25, 30},
    {35, 40, 45}
};

Memory Layout

Row Values
05 10 15
120 25 30
235 40 45

1.6.18 Displaying a Matrix

Example Program

#include <stdio.h>
 
int main()
{
    int mat[2][3] =
    {
        {1, 2, 3},
        {4, 5, 6}
    };
 
    int i, j;
 
    for(i = 0; i < 2; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("%4d", mat[i][j]);
        }
 
        printf("\n");
    }
 
    return 0;
}

Output

   1   2   3
   4   5   6

1.6.19 Matrix Addition

Example Program

#include <stdio.h>
 
int main()
{
    int A[2][2] = {{2,4},{6,8}};
    int B[2][2] = {{1,3},{5,7}};
    int C[2][2];
 
    int i, j;
 
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
 
    printf("Result Matrix:\n");
 
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            printf("%4d", C[i][j]);
        }
 
        printf("\n");
    }
 
    return 0;
}

Output

   3   7
  11  15

1.6.20 Character Arrays and Strings

Strings in C are stored as character arrays.

Example

char city[] = "Chennai";

Memory contains:

C h e n n a i \0

\0 is the null character that marks the end of a string.

1.6.21 Important Points About Arrays

Arrays are Zero Indexed

First element: arr[0]

Last element: arr[size - 1]

Fixed Size

Array size cannot change after declaration.

Contiguous Memory

Elements are stored one after another in memory.

Same Data Type

All elements must have the same data type.

1.6.22 Common Mistake: Array Out of Bounds

Incorrect

int arr[4] = {10,20,30,40};
 
printf("%d", arr[6]);

Result

Undefined behavior. Always access only valid indices.

1.6.23 Real-World Applications of Arrays

Arrays are used in:

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.